(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:xiframe
     * @description
     * # xiframe 

    用法：
    <xiframe ng-model="stockIn.warehouse_user_id"></xiframe>

     */
    angular.module('webApp')
        .directive('xiframe', ['dataService', '$compile', function(dataService, $compile) {
            return {
                restrict: 'E',
                replace: true,
                scope: {
                    url: '@',
                    title: '@',
                    callback: '&',
                    getUrl: '&',
                },
                template: "<button class='sui-btn btn-success' ng-click='showPop();'>{{title}}</button>",
                controller: ['helper', '$rootScope', '$scope', '$sce', function(helper, $rootScope, $scope, $sce) {
                    $scope.showPop = function() {
                        if (!$scope.url) {
                            if ($scope.getUrl) {
                                $scope.getUrl().then(function(curl) {
                                    if (curl) {
                                        $scope.surl = $sce.trustAsResourceUrl(curl);
                                        pop();
                                    }
                                });
                            } else {
                                return helper.myAlert("没有url或者没设置getUrl");
                            }
                        } else {
                            $scope.surl = $sce.trustAsResourceUrl($scope.url);
                            pop();
                        }
                    }

                    function pop() {
                        window.iframecallback = function(data) {
                            $scope.$apply(function() {
                                // console.error("iframecallback:", data,$scope.callback);
                                if ($scope.callback) {
                                    $scope.callback({ data: data });
                                }
                                winobj.modal("hide");
                            });
                        };

                        var winobj = $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '<form class="sui-form" style="margin:0px;"><span>{{"授权"|translate}}</span></form>',
                            body: '<div><iframe id="iframe" ng-src="{{surl}}" style="width:1250px;height:600px;" sandbox="allow-same-origin allow-scripts"></iframe></div>',
                            okBtn: '{{"ok"|translate}}',
                            closeBtn: true,
                            transition: true,
                            hasfoot: false,
                            width: '1100',
                            height: '600',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }
                }],
                link: function(scope, element, attrs, ctrl) {}
            }
        }]);
})();